/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nationsim.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import nationsim.api.Constants;
import nationsim.components.nation.NationState;

/**
 *  The Panel that will hold all the input for the user.
 * @author CJ
 */
public class UIPanel extends JPanel implements ActionListener{

    //UI instance fields
    protected JTextField[] textFields;
    protected JButton submitButton;

    public UIPanel(){
        this.initPanel();
        this.initUI();
    }

    protected void initPanel(){
        this.setLayout(new GridBagLayout());
        this.setPreferredSize(new Dimension(Constants.UI_PANEL_WIDTH, Constants.UI_PANEL_HEIGHT));
        this.setFocusable(true);
    }

    protected void initUI(){
        //Set up GridBag
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;

        this.textFields = new JTextField[Constants.UI_TEXT_INPUT_OPTIONS.length];

        int gridpos = 0;
        for(int i = 0; i < Constants.UI_TEXT_INPUT_OPTIONS.length; ++i){
            c.gridwidth = GridBagConstraints.RELATIVE;
            JLabel label = new JLabel(Constants.UI_TEXT_INPUT_OPTIONS[i]);
            ((GridBagLayout)this.getLayout()).setConstraints(label, c);
            this.add(label);
            c.gridwidth = GridBagConstraints.REMAINDER;

            JTextField currentField = new JTextField(Constants.UI_TEXT_INPUT_DEFAULT_VALUES[i], Constants.UI_PANEL_TEXT_FIELD_COL_NUM);
            ((GridBagLayout)this.getLayout()).setConstraints(currentField, c);
            this.add(currentField);
            this.textFields[i] = currentField;
            gridpos++;
        }
        c.gridheight = GridBagConstraints.REMAINDER;
        this.submitButton = new JButton("Submit");
        this.submitButton.setActionCommand("Submit");
        ((GridBagLayout) this.getLayout()).setConstraints(this.submitButton, c);
        this.add(this.submitButton);

        addActionListeners();
    }

    public void addListener(ActionListener toAdd){
        this.listenerList.add(ActionListener.class, toAdd);
    }

    protected void addActionListeners(){
        for(int i = 0; i < this.textFields.length; ++i)
            this.textFields[i].addActionListener(this);
        this.submitButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Submit")){
            for(ActionListener listener : this.listenerList.getListeners(ActionListener.class)){
                listener.actionPerformed(new ActionEvent(this, -1, e.getActionCommand()));
            }
        }
    }

    public String[] getTextField(){
        String[] result = new String[this.textFields.length];
        for(int i = 0; i < result.length; ++i){
            result[i] = this.textFields[i].getText();
        }
        return result;
    }

    public void refresh(NationState state){
        String[] toSet = state.getUserInputValues();
        for(int i = 0; i < toSet.length; ++i)
            this.textFields[i].setText(toSet[i]);
    }

}
